package com.ruoyi.web.core.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @BelongsProject: RuoYi-Vue-master
 * @BelongsPackage: com.ruoyi.web.core.config
 * @Author: chenjiajun
 * @CreateTime: 2023-12-19  10:49
 * @Description: TODO
 * @Email: chenjiajun56@foxmail.com
 * @Version: 1.0
 */
@Slf4j
@Configuration
@Component
public class MyMetaObjectHandlerConfig implements MetaObjectHandler {
    SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
    String now = sdf.format(new Date());
    /**
     * 创建人+创建时间
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject,"createBy", String.class, SecurityUtils.getUsername());
        this.strictInsertFill(metaObject,"createTime",String.class,now);
    }

    /**
     * 更新人＋更新时间
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject,"updateBy",String.class,SecurityUtils.getUsername());
        this.strictUpdateFill(metaObject,"updateTime",String.class,now);
    }
}
